﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Finance;
using AMM.Model;
using AMM.Model.Finance;

namespace AMM.Business.Finance {
    public class BankAccountProcessor : BaseProcessor {

        private int _pageSize;
        private BankAccountRepository _bankAccountRepository;

        public BankAccountProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._bankAccountRepository = new BankAccountRepository(pageSize);
        }

        public BankAccount FetchBankAccountById(int bankAccountId) {
            try {
                return this._bankAccountRepository.FetchBankAccountById(bankAccountId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定银行帐户失败！");
            }
        }

        public List<BankAccount> FetchBankAccountList() {
            try {
                return this._bankAccountRepository.FetchBankAccountList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取银行帐户失败！");
            }
        }

        public List<BankAccountViewModel> FetchBankAccountViewModelList(BankAccountSearchModel searchModel, int pageIndex = 1) {
            try {
                List<BankAccount> resultList = this._bankAccountRepository.FetchBankAccountList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new BankAccountViewModel() {
                        Key = x.BankAccountId,
                        Code = x.Code,
                        BankName = x.BankName,
                        BankAccountName = x.BankAccountName,
                        BankAccountNumber = x.BankAccountNumber,
                        CurrencyCode = x.Basic.Code,
                        FinancialAccountTypeCode = x.Basic1.Code,
                        OpenedDate = x.OpenedDate,
                        Remarks = x.Remarks,
                        IsEnabled = x.IsEnabled,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取银行帐户失败！");
            }
        }

        public void Insert(BankAccount bankAccount) {
            try {
                bankAccount.BankAccountId = Guid.NewGuid().GetHashCode();
                bankAccount.CreatedDatetime = DateTime.Now;
                this._bankAccountRepository.Insert(bankAccount);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新银行帐户失败！");
            }
        }

        public void Update(BankAccount bankAccount) {
            try {
                BankAccount oldBankAccount = this.FetchBankAccountById(bankAccount.BankAccountId);
                if (!oldBankAccount.RowVersion.SequenceEqual(bankAccount.RowVersion)) {
                    throw this.BuildException("更新银行帐户失败，数据不一致性错误！");
                }
                bankAccount.ModifiedDatetime = DateTime.Now;
                this._bankAccountRepository.Update(bankAccount);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新银行帐户失败！");
            }
        }

        public void Delete(BankAccount bankAccount) {
            try {
                this._bankAccountRepository.Delete(bankAccount);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除银行帐户失败！");
            }
        }

        public void Delete(params int[] bankAccountIds) {
            try {
                this._bankAccountRepository.DeleteMultipleBankAccount(bankAccountIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除银行帐户失败！");
            }
        }
    }
}
